package com.wn.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wn.bean.House;
import com.wn.bean.R;
import com.wn.mapper.HouseMapper;
import com.wn.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yellow Docter
 * @date 2022 -07-15
 * @desc
 */
@Service
public class HouseServiceImpl implements HouseService {


    @Autowired
    private HouseMapper houseMapper;

    /**
     *  查询所有
     * @return
     */
    @Override
    public R listHouse(Integer currentPage,Integer pageSize) {

        // 这里需要添加分页操作
        // 01 添加插件
        //02 需要配置
        Page<House> housePage = PageHelper.startPage(currentPage, pageSize).doSelectPage(() -> houseMapper.findAll());

        R ok = R.OK();
        Map<String,Object> map = new HashMap<>();

        // 查询的分页的数据
        List<House> result = housePage.getResult();
        // 分页的总数
        long total = housePage.getTotal();
        map.put("total",total);
        map.put("data",result);
        ok.setData(map);

        return ok;
    }

    @Override
    public R update(House house) {

        int row = houseMapper.update(house);

        return row >0 ? R.OK() : R.ERROR();
    }
}
